package com.fcl.blog.config;


import com.fcl.blog.config.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author 付
 * @Date 2023/10/12 11:16
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 指定controller 统一的接口前缀，是为了防止其他请求被拦截，例如swagger文档
        configurer.addPathPrefix("/api",clazz -> clazz.isAnnotationPresent(RequestMapping.class));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**") // 拦截的请求 /api/**   表示拦截api的所有，只有当jwt工具执行完成才能方向
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/admin/userLogin")
                .excludePathPatterns("/api/files/**"); // 不拦截的请求  如登录接口
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").addResourceLocations("file:///E:/Blog/vue_spriongboot/blog_springboot/file/");
    }
}
